package com.yuanian.timingwheel.model.task.method;

import com.yuanian.timingwheel.context.SpringContext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 任务执行的方法
 */
public class TaskMethod {

    //需要执行的方法
    private String executionMethod;

    private String beanName;

    //需要执行的入参
    private Object[] args;

    private Class[] paramClazz;

    public TaskMethod() {
    }

    public TaskMethod(String executionMethod, String beanName, Object[] args) {
        this.executionMethod = executionMethod;
        this.beanName = beanName;
        this.args = args;
        this.paramClazz = new Class[this.args.length];
        for (int i = 0; i < args.length; i++) {
            this.paramClazz[i] = args[i].getClass();
        }
    }

    public Object execute() throws InvocationTargetException, IllegalAccessException {
        Object obj = SpringContext.getBean(beanName);
        try {
            Method method = obj.getClass().getDeclaredMethod(executionMethod, paramClazz);
            return method.invoke(SpringContext.getBean(beanName), args);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getExecutionMethod() {
        return executionMethod;
    }

    public void setExecutionMethod(String executionMethod) {
        this.executionMethod = executionMethod;
    }

    public String getBeanName() {
        return beanName;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public Class[] getParamClazz() {
        return paramClazz;
    }

    public void setParamClazz(Class[] paramClazz) {
        this.paramClazz = paramClazz;
    }
}
